using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Solution.Application.Extensions;
using Solution.Application.Injection;
using Solution.Application.Modules;

namespace Solution.WebHost
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
            })
            .AddMvc(setupAction =>
            {
                setupAction.EnableEndpointRouting = false;
                setupAction.Filters.Add<RequestFilter>();
            });
            services.Configure<JwtAuthentication>(Configuration.GetSection("JwtAuthentication"));
            services.SetupAuthorization(Configuration.GetSection("JwtAuthentication").Get<JwtAuthentication>());
            services.AddMediatR();
            services.AddControllers();
            services.RegisterAutoMapper();
            services.RegisterHealthChecks();
            services.AddFluentValidation();
            services.AddOpenTracing(Configuration.GetSection("Jaeger").Get<JaegerOptions>());
            services.AddCAPRabbitMQ(Configuration.GetSection("RabbitMQ").Get<RabbitMQOption>());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseForwardedHeaders(new ForwardedHeadersOptions()
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            }); ;

            app.UseHttpsRedirection();

            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHealthChecks("/health");
            });
        }
    }
}